package com.pangu.ui.browser;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.pangu.AMApplication;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.ui.refresh.IPullable;

/**
 * Created by chenxunlin01 on 2015/11/24.
 */
public abstract class PullRecyclerViewItemBrowser<A extends AMApplication> extends RecyclerViewItemBrowser<A>
        implements IPullable {

    protected ARefreshViewController mController;

    public PullRecyclerViewItemBrowser(Context context, ARefreshViewController mController) {
        super(context);
        this.mController = mController;
    }

    public PullRecyclerViewItemBrowser(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean canPullDown() {
        int taskStatus = mTaskMark.getTaskStatus();
        if (taskStatus == ATaskMark.HANDLE_ERROR || taskStatus == ATaskMark.HANDLE_DOING) {
            return false;
        }

        if (taskStatus == ATaskMark.HANDLE_OVER && isLoadDataEmpty()) {
            return true;
        }

        View firstChild = recyclerView.getChildAt(0);
        if (firstChild != null && recyclerView.getChildAdapterPosition(firstChild) == 0
                && firstChild.getTop() == recyclerView.getPaddingTop()) {
            return true;

        } else {
            return false;
        }
    }

    @Override
    public boolean canPullUp() {
        return false;
    }

    @Override
    public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
        super.receiveResult(taskMark, exception, trackerResult);

        if (taskMark.getTaskType() == ATaskMark.TYPE_REFRESH) {
            if (taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
                mController.refreshFinish(ARefreshViewController.SUCCEED);

            } else {
                mController.refreshFinish(ARefreshViewController.FAIL);
            }
        }
    }
}
